package com.example.demo.service.admin.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.R.Result;
import com.example.demo.domin.Vo.ChartVo;
import com.example.demo.domin.Vo.Dieases;
import com.example.demo.domin.Vo.GenderVo;
import com.example.demo.domin.entity.Analytics;
import com.example.demo.functions.ChartFunctions;
import com.example.demo.mapper.ChartMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.admin.IChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ChartServiceImpl extends ServiceImpl<ChartMapper, Analytics> implements IChartService {
    @Autowired
    private ChartMapper chartMapper;
    @Autowired
    private UserMapper userMapper;
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public Result<List<ChartVo>> daily(String startDate, String endDate) throws ParseException {
        return apply(startDate,endDate,(chartVo,startdate)->{
            chartVo.setNew_users(userMapper.dailyUsers(startdate));
            chartVo.setActive_users(chartMapper.dailyChart(startdate));
            return chartVo;
        });
    }
    public Result<List<ChartVo>> apply(String startDate, String endDate,ChartFunctions chartFunctions) {
        LocalDate start = LocalDate.parse(startDate, formatter);
        LocalDate end = LocalDate.parse(endDate, formatter);
        List<ChartVo> list = new ArrayList<>();
        while (start.isBefore(end) || start.isEqual(end)) {
            ChartVo chartVo = new ChartVo();
            Date startdate = Date.from(start.atStartOfDay(ZoneId.systemDefault()).toInstant());
            //这里我希望由用户自己去实现
            ChartVo chartVo1 = chartFunctions.apply(chartVo,startdate);
            chartVo1.setDate(format.format(startdate));
            list.add(chartVo1);
            start = start.plusDays(1);
        }
        return new Result<>(list);
    }
//    private Result<List<ChartVo>> getListResult(String startDate, String endDate) {
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
//        LocalDate start = LocalDate.parse(startDate, formatter);
//        LocalDate end = LocalDate.parse(endDate, formatter);
//        List<ChartVo> list = new ArrayList<>();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//        while(start.isBefore(end) || start.isEqual(end)){
//            ChartVo chartVo = new ChartVo();
//            Date startdate = Date.from(start.atStartOfDay(ZoneId.systemDefault()).toInstant());
//            chartVo.setDate(format.format(startdate));
//            chartVo.setNew_users(userMapper.dailyUsers(startdate));
//            chartVo.setActive_users(chartMapper.dailyChart(startdate));
//            list.add(chartVo);
//            start=start.plusDays(1);
//        }
//        return new Result<>(list);
//    }



    @Override
    public Result<List<ChartVo>> activeUsers(String startDate, String endDate) {
        return apply(startDate,endDate,(chartVo,startdate)->{
            List<GenderVo> genderVoList = chartMapper.activeUsers(startdate);
            chartVo.setGenderVo(genderVoList);
            return chartVo;
        });
    }

    @Override
    public Result<List<ChartVo>> diseaseCount(String startDate, String endDate) {
        return apply(startDate, endDate, (chartVo,startdate)->{
            chartVo.setDieasesVo(chartMapper.diseaseCount(startdate));
            return chartVo;
        });
    }

    @Override
    public Result<List<Dieases>> hotDiseases() {

        return null;
    }
}
